var express = require('express');
var router = express.Router();
// var restApi = require('./restInterface');
const nedb = require('nedb');

const db = new nedb({
  filename: './data/mydata.db',
  autoload: true
});

// 获取全部数据集，如果传入datasetId，也可查询单个数据集
router.post('/getAllDataset', (req, res, next) => {
  var requestData = {
    type: "dataset"
  };
  if (req.body.datasetId) {
    requestData.datasetId = req.body.datasetId
  }
  db.find(requestData, (err, newDoc) => {
    if (err) {
      console.log(err)
      res.send({
        resultCode: 1,
        data: err
      });
    }
    else {
      console.log(newDoc)
      res.send({
        resultCode: 0,
        data: newDoc
      })
    }
  });
});

// 根据数据集Id获取数据
router.post('/queryDataByDatasetId', (req, res, next) => {
  var postData = {
    type: "data",
    datasetId: req.body.datasetId
  };
  db.findOne(postData, (err, newDoc) => {
    if (err) {
      console.log(err)
      res.send({
        resultCode: 1,
        data: err
      });
    }
    else {
      console.log(newDoc)
      res.send({
        resultCode: 0,
        data: newDoc.data
      })
    }
  });
});

// 保存业务模型结构
router.post('/saveModelStruct', (req, res, next) => {
  var postData = {
    type: "model",
    id: req.body.id,
    name: req.body.name,
    datasetId: req.body.datasetId,
    selectedItem: req.body.selectedItem,
    selectedItemType: req.body.selectedItemType,
    property: req.body.property
  }

  db.remove({ id: req.body.id }, { multi: true });

  db.insert(postData, (err, newDoc) => {
    if (err) {
      console.log(err),
        res.send({
          resultCode: 1,
          data: err
        });
    } else {
      console.log(newDoc)
      res.send({
        resultCode: 0,
        data: newDoc
      })
    }
    res.end();
  });
})

// 获取业务模型列表，传入id的话，获取某个业务模型
router.post('/deleteModel', (req, res, next) => {
  var postData = {
    id: req.body.id
  };

  db.remove(postData, { multi: true }, function (err, numRemoved) {
    if (err) {
      console.log(err)
      res.send({
        resultCode: 1,
        data: err
      });
    }
    else {
      res.send({
        resultCode: 0,
        data: numRemoved
      })
    }
    res.end();
  });
})

// 获取业务模型列表，传入id的话，获取某个业务模型
router.post('/getModelStruct', (req, res, next) => {
  var postData = {
    type: "model"
  };
  if (req.body.id) {
    requestData.id = req.body.id
  }

  db.find(postData, (err, newDoc) => {
    if (err) {
      console.log(err)
      res.send({
        resultCode: 1,
        data: err
      });
    }
    else {
      console.log(newDoc)
      res.send({
        resultCode: 0,
        data: newDoc
      })
    }
    res.end();
  });
})

// 保存构建页面
router.post('/savePage', (req, res, next) => {
  var postData = {
    type: "page",
    id: req.body.id,
    name: req.body.name,
    pageOption: req.body.pageOption,
    gridOption: req.body.gridOption,
  }

  db.remove({ id: req.body.id }, { multi: true });

  db.insert(postData, (err, newDoc) => {
    if (err) {
      console.log(err),
        res.send({
          resultCode: 1,
          data: err
        });
    } else {
      console.log(newDoc)
      res.send({
        resultCode: 0,
        data: newDoc
      })
    }
    res.end();
  });
})

// 删除构建的页面
router.post('/deletePage', (req, res, next) => {
  var postData = {
    id: req.body.id
  };

  db.remove(postData, { multi: true }, function (err, numRemoved) {
    if (err) {
      console.log(err)
      res.send({
        resultCode: 1,
        data: err
      });
    }
    else {
      res.send({
        resultCode: 0,
        data: numRemoved
      })
    }
    res.end();
  });
})

// 获取页面构建配置
router.post('/getPage', (req, res, next) => {
  var postData = {
    type: "page"
  };
  if (req.body.id) {
    postData.id = req.body.id
  }

  db.find(postData, (err, newDoc) => {
    if (err) {
      console.log(err)
      res.send({
        resultCode: 1,
        data: err
      });
    }
    else {
      console.log(newDoc)
      res.send({
        resultCode: 0,
        data: newDoc
      })
    }
    res.end();
  });
})

module.exports = router;